package com.neu.view.impl;

import com.neu.app.App;
import com.neu.dao.AdminDao;
import com.neu.dao.BussinessDao;
import com.neu.dao.impl.AdminDaoImpl;
import com.neu.dao.impl.BussinessDaoImpl;
import com.neu.util.Read;
import com.neu.view.UserView;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

//import static com.neu.util.Read.*;

public class AdminView extends UserView {
    private AdminDao adminDao = new AdminDaoImpl();
    private BussinessDao bussinessDao = new BussinessDaoImpl();


    @Override
    public Map<String,Object> login(){
        Map<String, Object> map = null;
        Scanner in = new Scanner(System.in);
        System.out.println("管理员账号：");
        String name = in.nextLine();
        System.out.println("管理员密码：");
        String pwd = in.nextLine();
        map = adminDao.login(name, pwd);
        return map;
    }

    @Override
    //返回登录页或者返回首页
    public void returnLevel() {
        int n = 0;
        while (n != 1 && n != 2 && n != 3) {
            System.out.println("1.返回首页");
            System.out.println("2.返回登录页");
            System.out.println("3.退出系统");
            n = Read.getIn().nextInt();
            switch (n) {
                case 1:
                    menu();
                    break;
                case 2:
                    App.main();
                    break;
                case 3:
                    System.exit(0);
                    break;
                default:
                    System.out.println("请选择功能！");
                    break;
            }
        }

    }

    @Override
    //管理员功能菜单
    public void menu() {
        System.out.println("管理员功能如下:");
        System.out.println("1.查询商家");
        System.out.println("2.添加商家");
        System.out.println("3.删除商家");
        System.out.println("4.退出系统");
        System.out.println("请选择功能:");
        int n = Read.getIn().nextInt();
        switch (n) {
            case 1:
                searchBussinessMenu();
                break;
            case 2:
                addBussinessMenu();
                break;
            case 3:
                deleteBussiness();
                break;
            case 4:
                System.exit(0);
                break;
            default:
                System.out.println("请选择正确的功能！");
                break;
        }

    }

    private void deleteBussiness() {
        System.out.println("请输入要删除的商家编号：");
        String bussinessId = Read.getIn().next();
        Map<String,Object> map= bussinessDao.findById(bussinessId);
        if(map==null){
            System.out.println("您输入的商品编号有误！");
            returnLevel();
        }
        System.out.println("商家编号\t商家名称\t商家地址\t起送价\t配送费\t商家状态");
        Object name = map.get("bussiness_name");
        Object addr = map.get("bussiness_address");
        Object startprice = map.get("bussiness_startprice");
        Object deliveryprice = map.get("bussiness_deliveryprice");
        Object status = map.get("bussiness_status");
        System.out.println(bussinessId+"\t"+name+"\t"+addr+"\t"+startprice+"\t"+deliveryprice+"\t"+status);
        System.out.println("是否删除？1.是||2.否");
        int n = Read.getIn().nextInt();
        if(n!=1){
            returnLevel();
        }
        int r = bussinessDao.deleteBussiness(bussinessId);
        if (r>0){
            System.out.println("删除成功！");
        }else {
            System.out.println("删除失败！");
        }
        returnLevel();
    }

    private void addBussinessMenu() {
        System.out.println("请输入商家信息：");
        System.out.println("商家名称：");
        String name = Read.getIn().next();
        System.out.println("商家地址：");
        String address = Read.getIn().next();
        System.out.println("商家简介：");
        String explain = Read.getIn().next();
        System.out.println("起送价：");
        String startprice = Read.getIn().next();
        System.out.println("配送费：");
        String peisongfei = Read.getIn().next();
        String pwd = "88888888";
        int row = bussinessDao.addBussiness(pwd, name, address, explain, startprice, peisongfei);
        if (row > 0) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
        returnLevel();
    }

    private void searchBussinessMenu() {
        System.out.println("1.查询所有商家");
        System.out.println("2.按照名称查询");
        System.out.println("请选择功能：");
        int n = Read.getIn().nextInt();
        switch (n) {
            case 1:
                searchBussinessAll();
                break;
            case 2:
                searchBussinessByName();
                break;

        }
    }

    private void searchBussinessAll() {
        List<Map<String, Object>> list = bussinessDao.findAll();
        System.out.println("商家编号\t商家名称\t商家地址\t起送费\t配送费\t状态");
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Object id = map.get("bussiness_id");
            Object name = map.get("bussiness_name");
            Object addr = map.get("bussiness_address");
            Object startprice = map.get("bussiness_startprice");
            Object deliveryprice = map.get("bussiness_deliveryprice");
            Object status = map.get("bussiness_status");
            System.out.println(id + "\t" + name + "\t" + addr + "\t" + startprice + "\t" + deliveryprice + "\t" + status);
        }
        returnLevel();
    }

    private void searchBussinessByName() {
        System.out.println("请输入商家名：");
        String n=Read.getIn().next();
        List<Map<String, Object>> list = bussinessDao.findByName(n);
        System.out.println("商家编号\t商家名称\t商家地址\t起送费\t配送费\t状态");
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Object id = map.get("bussiness_id");
            Object name = map.get("bussiness_name");
            Object addr = map.get("bussiness_address");
            Object startprice = map.get("bussiness_startprice");
            Object deliveryprice = map.get("bussiness_deliveryprice");
            Object status = map.get("bussiness_status");
            System.out.println(id + "\t" + name + "\t" + addr + "\t" + startprice + "\t" + deliveryprice + "\t" + status);
        }
        returnLevel();
    }

}
